package com.lovo.controller;

import com.lovo.dto.UserDTO;
import com.lovo.exception.MyException;
import com.lovo.model.UserDO;
import com.lovo.service.UserService;
import com.lovo.utils.ResultFactory;
import com.lovo.vo.ResultVO;
import org.omg.CORBA.UserException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @title
 * @author: luo
 * @date: 2020/11/17 16:55
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/login")
    public ResultVO login(@RequestBody UserDTO userDTO){
        UserDO login = userService.login(userDTO);
        return ResultFactory.success(login);
    }

    @PostMapping("")
    public ResultVO register(@RequestBody  UserDO userDO){
        System.out.println(userDO);
        userService.addUser(userDO);
        return ResultFactory.success();
    }

    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable("id") Integer userId){
        userService.deleteUser(userId);
        return ResultFactory.success();
    }

    @GetMapping("")
    public ResultVO allUser(){
        List<UserDO> all = userService.findAll();
        return ResultFactory.success(all);
    }

    @PutMapping("/{id}")
    public ResultVO update(@PathVariable("id") Integer id,@RequestBody UserDO userDO){
        userDO.setUserId(id);
        userService.updateUser(userDO);
        return ResultFactory.success();
    }

    @GetMapping("/{id}")
    public ResultVO findById(@PathVariable("id") Integer id){
        UserDO userDO = userService.findByUserId(id);
        return ResultFactory.success(userDO);
    }





}
